/**
 * Created by Administrator on 2016/8/3.
 */
"use strict";
swan_pc
    .controller("EventDetailController",function($scope,$timeout,EventManageById,candidateUserByEvent,UpdateEventById,ContactInfo,CommentManage,candidateUserManageById,ngDialog,candidateUserManage){
        $scope.activeNumber=1;
        $scope.changeActivityShow=function(option){
            $scope.activeNumber=option;
        }
        //cantact姓名展示
        $scope.contactCurrentInfo=[];
        ContactInfo.query(function(result){
            $scope.contactCurrentInfo=result;
        })
        $scope.contactNameShowOne=function(option){
            var contactName="";
            angular.forEach($scope.contactCurrentInfo,function(data,index,array){
                if(data.id==option){
                    contactName=data.baseInfo.name;
                }
            })
            return contactName;
        };
        //event着装要求展示
        $scope.eventDress_show=function(option){
            var name="";
            if(option=="CASUAL"){
                name="休闲";
            }else if(option=="BUSINESS_CASUAL"){
                name="商务休闲";
            }else if(option=="BUSINESS_FORMAL"){
                name="商务正装";
            }
            return name;
        };
        //eventType状态展示
        $scope.eventType_show=function(option){
            var statusName="";
            if(option=="CEREMONY"){
                statusName="典礼";
            }else if(option=="MEETING"){
                statusName="会议";
            }else if(option=="DINNER"){
                statusName="聚餐";
            }else if(option=="SPORTS"){
                statusName="体育";
            }else if(option=="ART"){
                statusName="艺术";
            }else if(option=="PARTY"){
                statusName="派对";
            }else if(option=="TRAINING"){
                statusName="培训";
            }else if(option=="OUTING"){
                statusName="户外运动";
            }else if(option=="SEMINAR"){
                statusName="研讨会";
            }else if(option=="RELEASE"){
                statusName="发布会";
            }else if(option=="CONCALL"){
                statusName="电话会议";
            }else if(option=="TOURS"){
                statusName="旅行";
            }else if(option=="OTHERS"){
                statusName="其他";
            }
            return statusName;
        };
        //event状态展示,user状态展示
        $scope.eventStatus_show=function(option){
            var statusName="";
            if(option=="DRAFT"){
                statusName="草稿";
            }else if(option=="CONFIRM"){
                statusName="确认";
            }else if(option=="CANCEL"){
                statusName="取消";
            }else if(option=="CLOSED"){
                statusName="关闭";
            }
            return statusName;
        }
        $scope.userEnrollStatus_show=function(option){
            var enrollStatusName="";
            if(option=="PENDING"){
                enrollStatusName="待定";
            }else if(option=="CONFIRM"){
                enrollStatusName="确认参加";
            }else if(option=="REJECT"){
                enrollStatusName="拒绝参加";
            }
            return enrollStatusName;
        }
        //获取Event基本信息
        $scope.currentEventInfo={};
        EventManageById.get({id:$scope.$parent.currentEventDetailId},function(result){
            $scope.currentEventInfo=result;
            $scope.currentEventInfo.startTime=new Date($scope.currentEventInfo.startTime);
            $scope.currentEventInfo.endTime=new Date($scope.currentEventInfo.endTime);
        })
        //双击编辑显示
        $scope.editLine=-1;
        $scope.EventDetailEdit=function(option){
            if($scope.currentEventInfo.status=="DRAFT"){
                $scope.editLine=option;
                var x=document.getElementById("id"+option);
                $timeout(function(){
                    x.focus();
                })
            }
        }
        $scope.show=function(index){
            var x=document.getElementById("show"+index);
            //alert(x.value);
            $timeout(function(){
                x.focus();
            });
        }
        //获取邀请人员信息
        $scope.initcandidateUsers=function() {
            $scope.candidateUserInCurrentEvent = [];
            $scope.candidateUserIdArray = [];
            candidateUserByEvent.query({id: $scope.$parent.currentEventDetailId}, function (result) {
                $scope.candidateUserInCurrentEvent = result;
                //console.log($scope.candidateUserInCurrentEvent);
                angular.forEach($scope.candidateUserInCurrentEvent, function (data, index, array) {
                    $scope.candidateUserIdArray.push(data.candidateId);
                })
            })
        }
        $scope.initcandidateUsers();
        //提交更新信息
        $scope.updateEventToDatebase=function(){
            UpdateEventById.update({},$scope.currentEventInfo,function(result){
                swal("更新成功","","success");
            },function(result){
                console.log(result);
                swal("更新失败","","error")
            })
        }
        //邀请人员管理
        $scope.candidateUser={
            activityId : null ,
            activityType :"INDIVIDUALEVENT",
            approveStatus : null, //邀请者批准状态 = ['PENDING', 'WAITING', 'CONFIRM', 'RESERVE', 'REJECT', 'CANCEL', 'FAIL'],
            approveTime:null, //邀请者批准时间 ,
            candidateId:null, //被邀请者的Member Id或UserContact Id ,
            candidateType:"USER_CONTACT", //被邀请者类型 = ['MEMBER', 'USER_CONTACT'],
            content : null,//内容，可自行填写；该字段为null时，后台自动填写对应活动的名称 ,
            enrollStatus: null,//被邀请者enroll状态 = ['PENDING', 'CONFIRM', 'REJECT', 'CANCEL', 'CONFLICT', 'FAIL'],
            enrollTime:null, //被邀请者enroll时间 ,
            hasUpdate:null, //是否有更新 ,
            inviter:null,    //邀请者，可自行填写；该字段为null时，后台自动填写组织名或邀请者姓名 ,
            lastRemindTime: null,//上一次提醒被邀请者的时间 ,
            //parentId (integer, optional),
            userId:null //被邀请者user id，由后台填写
        }
        $scope.candidateUser.activityId=$scope.$parent.currentEventDetailId;
        $scope.memberInvited=[];
        $scope.cancalThisCandidateUser=function(id){
            candidateUserManageById.delete({id:id},function(result){
                $scope.initcandidateUsers();
                swal("取消邀请此好友成功","","success");
            },function(result){
                swal("取消邀请此好友失败","","error");
            })
        }
        $scope.candidateUserInvite=function(i){
            $scope.candidateUser.candidateId=$scope.memberInvited[i];
            candidateUserManage.save($scope.candidateUser,function(result){
                //console.log(result);
                if(i<$scope.memberInvited.length-1){
                    i=i+1;
                    $scope.candidateUserInvite(i);
                }
            },function(result){
                console.log(result);
            })
        }
        $scope.AddCandidateUsers=function(){
            //$scope.newAddCandidateUser=[];
            $scope.memberInvited=[];
            //console.log($scope.candidateUserIdArray);
            //console.log($scope.candidateUserIdArray.length);
            ngDialog.open({
                templateUrl:"scripts/components/yue/organizebyme/dialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.selectedContact=[];
                    $scope.inputContactName="";
                    $scope.selectFlag=1;
                    $scope.ContactConfirm=function(){
                        $scope.$parent.memberInvited=$scope.selectedContact;
                        //console.log($scope.$parent.memberInvited);
                        $scope.candidateUserInvite(0);
                        $scope.initcandidateUsers();
                        ngDialog.closeAll();
                        swal("邀请好友成功","","success");
                    }
                }]
            })
        }
        $scope.$watch(function(){
            return $scope.memberInvited;
        },function(){
            $scope.contactNameShow($scope.memberInvited);
        })
        $scope.contactNameShow=function(idlist){
            var contactNameList="";
            for(var i=0;i<idlist.length;i++){
                for(var j=0;j<$scope.contactCurrentInfo.length;j++){
                    if(idlist[i]==$scope.contactCurrentInfo[j].id){
                        contactNameList=contactNameList+","+$scope.contactCurrentInfo[j].baseInfo.name;
                    }
                }
            }
            var length=contactNameList.length;
            contactNameList=contactNameList.substring(1,length);
            $scope.contactNameList=contactNameList;
        }
        //讨论模块实现
        $scope.newComment={
            activityId:0,
            activityType:"INDIVIDUALEVENT",
            content:null,
        }
        $scope.newComment.activityId=$scope.$parent.currentEventDetailId;
        $scope.allComments=[];
        $scope.initCommentDisplay=function() {
            $scope.commentsForCurrentEvent=[];
            CommentManage.query(function (result) {
                $scope.allComments = result;
                //console.log("allComments");
                //console.log($scope.allComments);
                angular.forEach($scope.allComments, function (data, index, array) {
                    //console.log($scope.$parent.currentEventDetailId);
                    //console.log(data.activityType + "     "+data.activityId );
                    if (data.activityType == "INDIVIDUALEVENT" && data.activityId == $scope.$parent.currentEventDetailId) {
                        $scope.commentsForCurrentEvent.push(data);
                    }
                })
            },function(result){
            })
            $scope.col = "time";
            $scope.desc = 0;
        }
        $scope.initCommentDisplay();
        $scope.submitComment=function(){
            CommentManage.save($scope.newComment,function(result){
                $scope.initCommentDisplay();
                swal("提交评论成功","","success");
            },function(result){
                //console.log(result);
                swal("提交评论失败","","error");
            })
        }
    })
